/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.brainstretch.recommend;

import java.io.Serializable;
import javax.persistence.Entity;
import org.brainstretch.models.User;
import org.brainstretch.models.quizzes.Category;

/**
 *
 * @author mrodaitis
 */
@Entity
public class UserProfile extends Profile implements Serializable {
    private static final long serialVersionUID = 1L;

    private final double MAX_WEIGHT = 1.0;
    private final double MIN_WEIGHT = 0.0;
    
    public UserProfile() {
        super();
    }
    
    public void adjustWeights(ProfileVector weightVector, double impact){
        ProfileVector thisVector = new ProfileVector(this);
        thisVector.normalizeInternal();
        for(ProfileDimension dim: ProfileDimension.values()){
            double weight = weightVector.getPropertyWeight(dim);
            double newValue = getNewValue(thisVector.getPropertyWeight(dim), weight, impact);
            newValue = clampValue(newValue);
            thisVector.setPropertyWeight(dim, newValue);
        }
        for(Category cat: Category.values()){
            double weight = weightVector.getCategoryWeight(cat);
            double newValue = getNewValue(thisVector.getCategoryWeight(cat), weight, impact);
            newValue = clampValue(newValue);
            thisVector.setCategoryWeight(cat, newValue);
        }
        setValuesTo(thisVector);
    }
    
    private double getNewValue(double original, double newWeight, double impact){
        double diff = newWeight - original;
        double newValue = impact * diff + original;
        return newValue;
    }
    
    private double clampValue(double value){
        value = Math.max(value, MIN_WEIGHT);
        value = Math.min(value, MAX_WEIGHT);
        return value;
    }
   
    @Override
    public String toString() {
        return "org.brainstretch.recommend.UserProfile[ id=" + getId() + " ]";
    }
    
}
